package sf.database.jdbc.type;

import java.net.URL;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.sql.Types;

public class UrlDATALINKType implements TypeHandler<URL> {
    public URL get(ResultSet rs, String colName) throws SQLException {
        return rs.getURL(colName);
    }

    public URL get(ResultSet rs, int index) throws SQLException {
        return rs.getURL(index);
    }

    @Override
    public URL get(CallableStatement cs, int index) throws SQLException {
        return cs.getURL(index);
    }

    @Override
    public URL get(CallableStatement cs, String parameterName) throws SQLException {
        return cs.getURL(parameterName);
    }

    @Override
    public void update(ResultSet rs, String columnLabel, Object value) throws SQLException {
        throw new SQLFeatureNotSupportedException("not support");
    }

    @Override
    public void update(ResultSet rs, int columnIndex, Object value) throws SQLException {
        throw new SQLFeatureNotSupportedException("not support");
    }

    public Object set(PreparedStatement stat, Object obj, int i) throws SQLException {
        if (null == obj) {
            stat.setNull(i, getSqlType());
        } else {
            stat.setURL(i, (URL) obj);
        }
        return null;
    }

    @Override
    public Object set(CallableStatement cs, Object obj, String parameterName) throws SQLException {
        if (null == obj) {
            cs.setNull(parameterName, getSqlType());
        } else {
            cs.setURL(parameterName, (URL) obj);
        }
        return null;
    }

    @Override
    public int getSqlType() {
        return Types.DATALINK;
    }

    @Override
    public Class<URL> getDefaultJavaType() {
        return URL.class;
    }
}
